package com.api.sw.duban.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.browser.bean.SearchConditionOption;
import com.api.sw.duban.model.Uf_jcdcrw;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import jxl.read.biff.Record;
import weaver.conn.RecordSet;
import weaver.docs.share.DocShareUtil;
import weaver.general.Util;
import weaver.hrm.User;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DubUtil {

    private final static LogTool log = new LogTool("/log/duban/",false);
    public final static String[] color=new String[]{"#E3F6D8","#FFE4C4","#FBDFEB"};//督办状态 背景颜色
    public final static String[] backgroundcolor=new String[]{"background-color: rgb(227, 246, 216);","background-color: rgb(255, 228, 196);","background-color: rgb(251, 223, 235);"};//督办状态 背景颜色
    public final static String[] backgroundcolorpng=new String[]{"/swiconfont/green.png","/swiconfont/yellow.png","/swiconfont/red.png"};//督办状态 背景颜色
    public final static String modeId="132";//""11";//模块
    public final static String customid="227";//""34";//页面ID
    public final static String formId="-227";//""-5";//表单ID

    public final static String jcmodeId="155";//""15";//决策通知模块
    //public final static String jccustomid="34";//页面ID
    public final static String jcformId="-264";//""-15";//表单ID

    public final static String wf="151";//43+"";
    /*
        获取文号
     */
    public  static JSONObject getWh(){

        RecordSet re=new RecordSet();
        JSONObject json=new JSONObject();
        String mess="";
        int whid=-1;
        boolean flag=true;
        try {
            WhUtil whUtil=WhUtil.getInstance();
            whid= whUtil.getNewRequestId();
        } catch (Exception e) {
            e.printStackTrace();
            flag=false;
            mess=e.getMessage();
        }
        json.put("flag",flag);
        json.put("mess",mess);
        json.put("whid",whid);
        return json;
    }
    /*
        获取部门督查对接人
     */
    public static String getBmDefindByid(String bmstr){
        String str="";
        bmstr= Util.null2String(bmstr).trim();
        if("".equals(bmstr)){
            return "";
        }
        if(bmstr.endsWith(",")){
            bmstr=bmstr.substring(0,bmstr.length()-1);
        }
        RecordSet re=new RecordSet();
        String sql="select dbfzr from hrmdepartmentdefined where deptid in ("+bmstr+")";
        re.execute(sql);
        while(re.next()){
            String dbfzr=Util.null2String(re.getString("dbfzr"));
            if(dbfzr.endsWith(",")){
                str=str+dbfzr;
            }else{
                str=str+dbfzr+",";
            }
        }
        if(str.endsWith(",")){
            str=str.substring(0,str.length()-1);
        }
        return str;
    }
    /*
        通过收文单位获取对应的人
     */
    public static String getDcryByReceiveunit(String Receiveunit){
        RecordSet re=new RecordSet();
        String str="";
        Receiveunit=Util.null2String(Receiveunit).trim();
        if(Receiveunit.endsWith(",")){
            Receiveunit=Receiveunit.substring(0,Receiveunit.length()-1);
        }
        String sql="select dczy from uf_sfwdwdyb where sfwdw in ("+Receiveunit+")";
        re.execute(sql);
        while(re.next()){
            String dczy=Util.null2String(re.getString("dczy")).trim();
            if("".equals(dczy)){
                continue;
            }
            if(dczy.endsWith(",")){
                dczy=dczy.substring(0,dczy.length()-1);
            }
            str=str+dczy+",";
        }
        if(str.endsWith(",")){
            str=str.substring(0,str.length()-1);
        }
        return str;
    }
    //通过人员的ID获取对应的 浏览按钮的格式

    public static JSONObject getHrmspecialobj(String hrmid){
        hrmid=Util.null2String(hrmid).trim();
        JSONObject result=new JSONObject();
        JSONObject json=null;
        JSONArray Specialobj=new JSONArray();
        if("".equals(hrmid)){
            return result;
        }else{
            if(hrmid.endsWith(",")){
                hrmid=hrmid.substring(0,hrmid.length()-1);
            }
        }
        RecordSet re=new RecordSet();
        String sql=" select id,lastname from hrmresource where id in ("+hrmid+") "+
                   " union all "+
                   " select id,lastname from hrmresourcemanager where  id in ("+hrmid+")";
        re.execute(sql);
        while(re.next()){
            json=new JSONObject();
            String id=Util.null2String(re.getString("id")).trim();
            String lastname=Util.null2String(re.getString("lastname"));
            json.put("id",id);
            json.put("name",lastname);
            Specialobj.add(json);
        }
        result.put("value",hrmid);
        result.put("specialobj",Specialobj);
        return result;
    }
    //日期范围
    ////1 今天:2本周:3本月:4本季度:5本年:6时间区间:7上年:8:上个月
    public static String getRQfW(String type,String field){
        String sql="";
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
        if("1".equals(type)){
            sql=field+"='"+sim.format(new Date())+"'";
        }else if("2".equals(type)){
            sql=field+">='"+getWeekStart()+"' and "+field+"<='"+getWeekEnd()+"'";
        }else if("3".equals(type)){
            sql=field+">='"+getMonthStart()+"' and "+field+"<='"+getMonthEnd()+"'";
        }else if("4".equals(type)){
            int Quarter=getQuarter();
            String [] CurrQuarter=getCurrQuarter(Quarter);
            if(CurrQuarter!=null&&CurrQuarter.length==2){
                sql=field+">='"+CurrQuarter[0]+"' and "+field+"<='"+CurrQuarter[1]+"'";
            }

        }else if("5".equals(type)){
            sql=field+">='"+getYearStart()+"' and "+field+"<='"+getYearEnd()+"'";
        }else if("7".equals(type)){
            sql=field+">='"+getProMothedStart()+"' and "+field+"<='"+getProMothedEnd()+"'";

        }else if("8".equals(type)){
            //getProMothedStart
            int currentYear=getSysYear()-1;
            sql=field+">='"+getYearFirst(currentYear)+"' and "+field+"<='"+getYearLast(currentYear)+"'";
        }
        return sql;
    }
    /**
     * 获取昨天
     * @return String
     * */
    public static String getYestoday(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    /**
     * 获取本月开始日期
     * @return String
     * **/
    public static String getMonthStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    /**
     * 获取本月最后一天
     * @return String
     * **/
    public static String getMonthEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    /**
     * 获取本周的第一天
     * @return String
     * **/
    public static String getWeekStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    /**
     * 获取本周的最后一天
     * @return String
     * **/
    public static String getWeekEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    /**
     * 获取本年的第一天
     * @return String
     * **/
    public static String getYearStart(){
        return new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
    }

    /**
     * 获取本年的最后一天
     * @return String
     * **/
    public static String getYearEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast);
    }

    /**
     * 获取某季度的第一天和最后一天
     *	@param num 第几季度
     */
    public static String[] getCurrQuarter(int num) {
        String[] s = new String[2];
        String str = "";
        // 设置本年的季
        Calendar quarterCalendar = null;
        switch (num) {
            case 1: // 本年到现在经过了一个季度，在加上前4个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 3);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = formatDate(new Date(), "yyyy-MM-dd");
                s[0] = str.substring(0, str.length() - 5) + "01-01";
                s[1] = str;
                break;
            case 2: // 本年到现在经过了二个季度，在加上前三个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 6);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = formatDate(new Date(), "yyyy-MM-dd");
                s[0] = str.substring(0, str.length() - 5) + "04-01";
                s[1] = str;
                break;
            case 3:// 本年到现在经过了三个季度，在加上前二个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 9);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = formatDate(new Date(), "yyyy-MM-dd");
                s[0] = str.substring(0, str.length() - 5) + "07-01";
                s[1] = str;
                break;
            case 4:// 本年到现在经过了四个季度，在加上前一个季度
                quarterCalendar = Calendar.getInstance();
                str = formatDate(new Date(), "yyyy-MM-dd");
                s[0] = str.substring(0, str.length() - 5) + "10-01";
                s[1] = str.substring(0, str.length() - 5) + "12-31";
                break;
        }
        return s;
    }

    /**
     * 用途：以指定的格式格式化日期字符串
     * @param pattern 字符串的格式
     * @param currentDate 被格式化日期
     * @return String 已格式化的日期字符串
     * @throws NullPointerException 如果参数为空
     */
    public static String formatDate(Date currentDate, String pattern){
        if(currentDate == null || "".equals(pattern) || pattern == null){
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(currentDate);
    }
    /**
     * 获取当前季度
     *
     */
    public static int getQuarter() {
        Calendar c = Calendar.getInstance();
        int month = c.get(c.MONTH) + 1;
        int quarter = 0;
        if (month >= 1 && month <= 3) {
            quarter = 1;
        } else if (month >= 4 && month <= 6) {
            quarter = 2;
        } else if (month >= 7 && month <= 9) {
            quarter = 3;
        } else {
            quarter = 4;
        }
        return quarter ;
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static String getYearFirst(int year){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return format.format(currYearFirst);
    }
    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static String getYearLast(int year){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return format.format(currYearLast);
    }

    public static String getProMothedStart(){

        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar=Calendar.getInstance();

        calendar.add(Calendar.MONTH, -1);

        calendar.set(Calendar.DAY_OF_MONTH, 1);

        System.out.println("上个月第一天："+format.format(calendar.getTime()));
        return format.format(calendar.getTime());

    }

//获取上个月的最后一天

    public static String getProMothedEnd(){

        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar=Calendar.getInstance();

        int month=calendar.get(Calendar.MONTH);

        calendar.set(Calendar.MONTH, month-1);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

        System.out.println("上个月最后一天："+sf.format(calendar.getTime()));
        return sf.format(calendar.getTime());

    }
    public static int getSysYear() {
        Calendar date = Calendar.getInstance();
        int year = Integer.parseInt(String.valueOf(date.get(Calendar.YEAR))) ;

        return year;
    }
    public static void main1(String[] arg){
        System.out.println(DubUtil.getRQfW("1","substr(modedatacreatedate,0,10)")); ;
    }
    /*
        获取任务的创建人
     */
    public static int getRwCreateId(String rwid){
        int modedatacreater=0;
        RecordSet re=new RecordSet();
        String sql="select modedatacreater from uf_dcsxzb where id='"+rwid+"'";
        re.execute(sql);
        if(re.next()){
            modedatacreater=Util.getIntValue(re.getString("modedatacreater"),0) ;
        }
        return modedatacreater;
    }
    /*
        获取任务的创建人
     */
    public static String getRwCkry(String rwid){
        String ckry="";
        RecordSet re=new RecordSet();
        String sql="select ckry from uf_dcsxzb where id='"+rwid+"'";
        re.execute(sql);
        if(re.next()){
            ckry=Util.null2String(re.getString("ckry")).trim() ;
        }
        return ckry;
    }
    //人员更新到  任务查看人员
    public  static boolean updRy(String rwid,String ry){
        boolean flag=true;
        ry=Util.null2String(ry).trim() ;
        rwid=Util.null2String(rwid).trim() ;
        if("".equals(ry)||"".equals(rwid)){
            return true;
        }
        String yckry= getRwCkry(rwid);
        if(yckry.endsWith(",")){
            yckry=yckry.substring(0,yckry.length()-1);
        }
        log.writeLog(rwid+"updRy"+yckry+" --- "+ry);
        RecordSet re=new RecordSet();
        String[] rys=ry.split(",");
        for(String str:rys){
            int usrid= Util.getIntValue(Util.null2String(str).trim(),0)   ;
            if(usrid>0){
                if((","+yckry+",").indexOf((","+usrid+","))<0){
                    if(yckry.length()>0){
                        yckry=yckry+","+usrid;
                    }else{
                        yckry=usrid+"";
                    }

                }
            }
        }
        if(yckry.length()>0){
            flag=re.execute("update uf_dcsxzb set ckry='"+yckry+"'  where id='"+rwid+"'");
        }
        return flag;
    }
    /*
        根据任务 更新表单附件
     */
    public static void UpdRwFj(String rwid,String fj){
        RecordSet re=new RecordSet();
        RecordSet re1=new RecordSet();
        String oldfj="";
        String sql="select scfj from uf_dcsxzb where id='"+rwid+"'";
        re.execute(sql);
        if(re.next()){
            oldfj=Util.null2String(re.getString("scfj")).trim();
        }
        if("".equals(oldfj)){
            oldfj=fj;
        }else{
            if(oldfj.endsWith(",")){
                oldfj=oldfj.substring(0,oldfj.length()-1);
            }
            oldfj=oldfj+","+fj;
        }
        log.writeLog("UpdRwFj  update uf_dcsxzb set scfj='"+oldfj+"' where id="+rwid);
        re1.execute("update uf_dcsxzb set scfj='"+oldfj+"' where id="+rwid);
    }
    /*
        根据任务ID获取主办单位
     */
    public static String getRwZbdw(String rwid){
        String zbdw="";
        RecordSet re=new RecordSet();
        String sql="select zbdw from uf_dcsxzb where id='"+rwid+"'";
        re.execute(sql);
        if(re.next()){
            zbdw=Util.null2String(re.getString("zbdw")).trim() ;
        }
        return zbdw;
    }
    /*
        根据任务ID获取主办单位
     */
    public static String getRwXbdw(String rwid){
        String xbdw="";
        RecordSet re=new RecordSet();
        String sql="select xbdw from uf_dcsxzb where id='"+rwid+"'";
        re.execute(sql);
        if(re.next()){
            xbdw=Util.null2String(re.getString("xbdw")).trim() ;
        }
        return xbdw;
    }
    /*
        根据任务ID获取 自定义自定字段
     */
    public static String getRwCustId(String rwid,String field){
        String fieldval="";
        RecordSet re=new RecordSet();
        String sql="select "+field+" from uf_dcsxzb where id='"+rwid+"'";
        re.execute(sql);
        if(re.next()){
            fieldval=Util.null2String(re.getString(field)).trim() ;
        }
        return fieldval;
    }
    /*
        附件共享
     */
    public static void GxFj(String fileid,String userid){

        fileid=Util.null2String(fileid).trim();
        userid=Util.null2String(userid).trim();
        if("".equals(fileid)||"".equals(userid)){
            return;
        }
        String[] fileids=fileid.split(",");
        for(String str:fileids){
            str=Util.null2String(str).trim();
            if(!"".equals(str)){
                int docuser= Util.getIntValue(FileUtil.getFileCreaterByDocid(str),0) ;
                if(docuser>0){
                    User user=new User(docuser);
                    DocShareUtil.addDocShare(user,str,"1",userid,"0","1");
                }
            }
        }
    }
    /*
        跟新主办单位 或者协办单位
     */
    public static void updDw(String rwid,String zbdw,String xbdw){
        RecordSet re=new RecordSet();
        String sql="update uf_dcsxzb set zbdw='"+zbdw+"',xbdw='"+xbdw+"'  where id='"+rwid+"'";
        re.execute(sql);

    }
    /*
        合并
     */
    public static String SetHb(String src,String tag){
        src=Util.null2String(src).trim();
        tag=Util.null2String(tag).trim();
        if("".equals(tag)){
            return src;
        }
        String[] tags=tag.split(",");
        for(String demo:tags){
            demo=Util.null2String(demo).trim();
            if("".equals(demo)){
                continue;
            }
            if((","+src+",").indexOf((","+demo+","))>=0){//存在

            }else{
                if(src.endsWith(",")){
                    src=src+demo;
                }else{
                    if("".equals(src)){
                        src=demo;
                    }else{
                        src=src+","+demo;
                    }
                }
            }

        }
        return src;
    }
    /*
        通过交办记录ID获取 相关的人员  uf_dbjblb  dw  dbry
     */
    public static String getXgryByJbjl(String jbjlid){
        RecordSet re=new RecordSet();
        String newry="";
        String sql="select * from uf_dbjblb where id="+jbjlid;
        re.execute(sql);
        while(re.next()){
            newry=Util.null2String(re.getString("djr")).trim();
            //String ry=Util.null2String(getDcryByReceiveunit(dw));
            if(!"".equals(newry)){
                if(newry.endsWith(",")){
                    newry=newry.substring(0,newry.length()-1);
                }
            }
            String dbry=Util.null2String(re.getString("dbry")).trim();
            if(!"".equals(dbry)){
                newry=newry+ ","+dbry;
            }

        }
        return newry;
    }
    /*
        获取自定义的列
     */
    public static List<String> getTableListCustomBypid(String pageuid,String userid){
        List<String> list=new ArrayList<String>();
        RecordSet re=new RecordSet();
        String sql="select DATAINDEX from cloudstore_defcol  where pageuid='"+pageuid+"' and userid='"+userid+"' order by  orders";
        re.execute(sql);
        while(re.next()){
            String dataindex=Util.null2String("dataindex").trim();
            list.add(dataindex);
        }
        return list;

    }
    /*
        获取人员对应的主办单位

     */
    public  static String getZbdwByModelRy(String userid){
        String sql="";
        String zbdw="";
        RecordSet re=new RecordSet();
        //where  ','+cast(dczy as varchar(4000))+',' like '%,26,%'
        String dbtype=re.getDBType();
        if("sqlserver".equals(dbtype)){
            sql="select * from  uf_sfwdwdyb where  ','+cast(dczy as varchar(4000))+',' like '%,"+userid+",%'";
        }else{
            sql=" select * from  uf_sfwdwdyb where  ','||to_char(substr(dczy,1,1000))||',' like '%,"+userid+",%'";
        }
        re.execute(sql);
        while(re.next()){
            String sfwdw=Util.null2String(re.getString("sfwdw")).trim();
            if("".equals(sfwdw)){
                continue;
            }
            if(sfwdw.endsWith(",")){
                sfwdw.substring(0,sfwdw.length()-1);
            }
            zbdw=zbdw+sfwdw+",";
        }
        if(zbdw.endsWith(",")){
            zbdw.substring(0,zbdw.length()-1);
        }
        if("".equals(zbdw)){
            zbdw="0";
        }
        return zbdw;
    }
    /*
        获取决策督办事项的表 规则设置字段
     */
    public static List<String> getCustFeild(String tzid){
        RecordSet re=new RecordSet();
        List<String> list=new ArrayList<>();
        String sql=" select * from uf_jcgzsd where ssdctz='"+tzid+"'";
        re.execute(sql);
        while(re.next()){
            int len=0;
            String check1=Util.null2String(re.getString("check1"));
            String check2=Util.null2String(re.getString("check2"));
            String check3=Util.null2String(re.getString("check3"));
            String check4=Util.null2String(re.getString("check4"));
            if("1".equals(check4)&&len==0){
                len=4;
            }
            if("1".equals(check3)&&len==0){
                len=3;
            }
            if("1".equals(check2)&&len==0){
                len=2;
            }
            if("1".equals(check1)&&len==0){
                len=1;
            }
            for(int i=0;i<len;i++){
                String bt=Util.null2String(re.getString("bt"+(i+1))).trim();
                if("".equals(bt)){
                    bt="事项";
                }
                list.add(bt);
            }
        }
        return list;
    }
    /*
        获取决策督察类别
     */
    public static List<SearchConditionOption> getSelectnameByValue(String tablename,String field){
        List<SearchConditionOption> typeOptions = new ArrayList <SearchConditionOption>();  //设置选项值
        typeOptions.add(new SearchConditionOption("","全部",true));
        RecordSet re=new RecordSet();
        String sql="select c.selectname from workflow_bill a,workflow_billfield b,workflow_selectitem c where a.id=b.billid and a.tablename='"+tablename+"'" +
                " and b.id=c.fieldid and b.fieldname='"+field+"' ";
        re.execute(sql);
        while(re.next()){
            String selectname;
            String selectvalue=Util.null2String(re.getString("selectvalue"));
            selectname=Util.null2String(re.getString("selectname"));
            typeOptions.add(new SearchConditionOption(selectvalue,selectname));
        }
        return typeOptions;
    }
    /*
        最大长度限制
     */
    public static int getMaxLength(){
        RecordSet re=new RecordSet();
        String sql="select * from uf_pzcs  ";
        re.execute(sql);
        if(re.next()){
            return Util.getIntValue(re.getString("zdcd"),-1);
        }else{
            return -1;
        }
    }
    public static String getMessgetype(){
        RecordSet re=new RecordSet();
        String sql="select * from uf_pzcs  ";
        re.execute(sql);
        if(re.next()){
            return Util.null2String(re.getString("txid"),"");
        }else{
            return "";
        }
    }
    /*
        获取延迟
     */
    public static int getMin(){
        RecordSet re=new RecordSet();
        String sql="select * from uf_pzcs  ";
        re.execute(sql);
        if(re.next()){
            return Util.getIntValue(re.getString("ychm"),1000);
        }else{
            return -1;
        }
    }
    /*
        通过任务获取 层级的 domid
        level 设置的递归级别  不等于了的话 就要报错 超出了
     */
    public static String setListDomid(List<Object> list,String mainDomid,String newDomid,String wfkey,int level,String yssdctz,String newssdctz,User user ){
        String newLevelDomid="";// 返回必须要有值  返回为空表示 数据异常了
        String supid=newDomid;
        Uf_jcdcrw uf_jcdcrw=null;
        RecordSet re=new RecordSet();
        String sql="select *,level from uf_jcdcrw where ssdctz="+yssdctz+" and domid!=1   start with domid='"+mainDomid+"' " +
                    " connect by prior sssj=domid order by level ";
        log.writeLog(sql);
        re.execute(sql);
        // 拼接  level sup bh
        JSONObject domidjson=getLevelBylevel(level);
        log.writeLog(level+domidjson.toJSONString());
        if(domidjson==null||domidjson.size()<=0){
            log.writeLog(level+"层级json为空");
            return "";
        }
        newLevelDomid=domidjson.getString(1+"");
        int bhlen=1;
        int levellen=0;
        while(re.next()){
            levellen++;
            String id=Util.null2String(re.getString("id"));//任务id
            String levelstr=Util.null2String(re.getString("level"));//层级
            if("1".equals(levelstr)){//放弃第一层  因为只需要其他的层级
                continue;
            }
            uf_jcdcrw=new Uf_jcdcrw();

            String bh=Util.null2String(re.getString("bh"))+"_"+bhlen;
            String gzrw=Util.null2String(re.getString("gzrw"));
            String qtdw=Util.null2String(re.getString("qtdw"));
            String lb=Util.null2String(re.getString("lb"));
            //String zt=Util.null2String(re.getString("bh"));
            String currentLevelDomid=Util.null2String(domidjson.getString(levelstr)).trim() ;
            if("".equals(currentLevelDomid)){
                log.writeLog(currentLevelDomid+"层级currentLevelDomid为空");
                return "";
            }
            String ssdctz=newssdctz;//Util.null2String(re.getString("ssdctz"));//新的通知ID
            //采用方法的形式防止数组越界
            String[] currentLevelDomidall=currentLevelDomid.split("-");
            String newsssj="";
            String newdomid="";
            if(currentLevelDomidall.length==1){

                newsssj="1";//默认第一个通知任务
                newdomid=currentLevelDomid;
            }else{
                newsssj=currentLevelDomidall[currentLevelDomidall.length-2];
                newdomid=currentLevelDomidall[currentLevelDomidall.length-1];
            }
            String sssj=Util.null2String(newsssj);
            String levelids=Util.null2String(currentLevelDomid);
            String domid=Util.null2String(newdomid);
            log.writeLog("currentLevelDomid--->"+currentLevelDomid);
            log.writeLog("sssj--->"+sssj);
            log.writeLog("domid--->"+domid);

            uf_jcdcrw.setSfsrw(1);
            uf_jcdcrw.setQtdw(qtdw);
            uf_jcdcrw.setWfkey(wfkey);
            uf_jcdcrw.setSssj(sssj);
            uf_jcdcrw.setLevelids(levelids);
            uf_jcdcrw.setDomid(domid);
            uf_jcdcrw.setGzrw(gzrw);
            uf_jcdcrw.setBh(bh);
            uf_jcdcrw.setLb(lb);
            uf_jcdcrw.setYyrwid(id);
            uf_jcdcrw.setSsdctz(newssdctz);
            uf_jcdcrw.setModedatacreater(user.getUID()+"");
            list.add(uf_jcdcrw);
            bhlen++;
        }
        for(Object uf_jcdcrw1:list){
            Uf_jcdcrw uf_jcdcrw11=(Uf_jcdcrw)uf_jcdcrw1;
            if(uf_jcdcrw11!=null){
                String Levelids=uf_jcdcrw11.getLevelids();
                String newdomid=uf_jcdcrw11.getDomid();
                if(!"".equals(Levelids)&&!"1".equals(newdomid)){
                    Levelids="1-"+Levelids;
                }
                uf_jcdcrw11.setLevelids(Levelids);
            }
        }
        if(level!=levellen){
            log.writeLog(level+"层级不对"+levellen);
            return "";
        }
        if(list.size()>0){

        }
        return newLevelDomid;
    }
    /*
        通过层级以及 domid自动生成 level
     */
    public static JSONObject getLevelBylevel(int level){
        JSONObject leveljson=new JSONObject();
        String begin="";
        long key=System.currentTimeMillis();
        for(int i=level;i>0;i--){
            begin=begin+"-"+(key+1);
            if(begin.startsWith("-")){
                begin=begin.substring(1);
            }
            leveljson.put(i+"",begin);
            key++;
        }
        return leveljson;
    }
    /*
        发送消息
     */
    public static void SendMessage(int creater,String type ,Set<String> userIdList,String title,String context){
        MessageType messageType = MessageType.newInstanceById(type); // 消息来源，暂定广播类型，需要连接数据库，否则报错
        String linkUrl = ""; // PC端链接
        String linkMobileUrl = ""; // 移动端链接
        try {
            MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl,
                    linkMobileUrl);
            if(creater<=0){
                creater=1;
            }
            messageBean.setCreater(creater);// 创建人id
            Util_Message.store(messageBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void SynSendMessage(int creater,String type ,Set<String> userIdList,String title,String context){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    DubUtil.SendMessage( creater, type , userIdList, title, context);
                }
            }).start();
    }
    public static void main(String[] arg){
           Random r=new Random(100);
           String demo="1603892179400-1603892179401";
        demo=demo.substring(0,demo.lastIndexOf("-")+1)+"2313123";
        System.out.println( demo);
        String newsssj=demo.split("-")[demo.split("-").length-2];
            System.out.println( newsssj);
    }
}
